using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Modularity;
using Volo.Abp.Threading;
using Volo.Abp.AspNetCore.Mvc;
using Microsoft.AspNetCore.Builder;

namespace CKY.AgentPlatform.RateLimiting
{
    /// <summary>
    /// 限流模块
    /// </summary>
    [DependsOn(
        typeof(AbpAspNetCoreMvcModule)
    )]
    public class AgentPlatformRateLimitingModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            AsyncHelper.RunSync(async () =>
            {
                await ConfigureRateLimitingServicesAsync(context);
            });
        }

        private async Task ConfigureRateLimitingServicesAsync(ServiceConfigurationContext context)
        {
            var configuration = context.Services.GetConfiguration();
            
            // 配置限流选项
            // context.Services.Configure<RateLimitOptions>(configuration.GetSection("RateLimit"));

            // 注册限流中间件
            // context.Services.AddScoped<RateLimitingMiddleware>();
            // context.Services.AddScoped<RateLimitingService>();
        }

        // public override void OnApplicationInitialization(ApplicationInitializationContext context)
        // {
        //     var app = context.GetApplicationBuilder();

        //     // 使用限流中间件
        //     app.UseMiddleware<RateLimitingMiddleware>();
        // }
    }
}